package com.yanqu.road.server.manger.activity.chefduel;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.ChefDuelData;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2Dish;
import com.yanqu.road.entity.activity.cookboy.process2.PickDishRes;
import com.yanqu.road.entity.enums.activity.chefDuel.eChefDuelActivityType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 厨神争霸mgr
 */
public class ChefDuelMgr extends TempMgr {

    private static Logger log = LogManager.getLogger(ChefDuelMgr.class.getName());

    private static Map<Integer,ChefDuel1> chefDuel1Map = new ConcurrentHashMap<>();

    private static Map<Integer,ChefDuel2> chefDuel2Map = new ConcurrentHashMap<>();

    private static Map<Integer,ChefDuel3> chefDuel3Map = new ConcurrentHashMap<>();

    private static Map<Integer, Object> activityLock;

    public static void loadActivity(int activityId) {
        synchronized (getActivityLock(activityId)) {
            ActivityInfo activityInfo = ChefDuelActivityMgr.getActivityInfo(activityId);
            if (activityInfo != null) {
                if (activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()){

                }else if (activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()){
                    if (!chefDuel2Map.containsKey(activityId)) {
                        ChefDuel2 c2 = new ChefDuel2(activityInfo);
                        if (c2.reload()){
                            chefDuel2Map.put(activityId, c2);
                        }else {
                            log.error("厨神争霸二阶段" + activityId + "加载失败");
                        }
                    }
                }else if (activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()){
                    if (!chefDuel3Map.containsKey(activityId)) {
                        ChefDuel3 c3 = new ChefDuel3(activityInfo);
                        if (c3.reload()){
                            chefDuel3Map.put(activityId, c3);
                        }else {
                            log.error("厨神争霸二阶段" + activityId + "加载失败");
                        }
                    }
                }
            }else {
                log.error("厨神争霸" + activityId + "活动activityInfo加载失败");
            }
        }
    }

    /**
     * 移除过期活动
     */
    public static void removeActivity(){
        Map<Integer, ActivityInfo> activityInfoMap = RelationActivityMgr.getOpenActivityInfoMap(eActivityType.ChefDuel.getValue());
        for (ChefDuel1 chefDuel1:chefDuel1Map.values()){
            int activityId = chefDuel1.getActivityInfo().getActivityId();
            if (!activityInfoMap.containsKey(activityId)) {
                removeChefDuelData(activityId);
            }
        }
        for (ChefDuel2 chefDuel2:chefDuel2Map.values()){
            int activityId = chefDuel2.getActivityInfo().getActivityId();
            if (!activityInfoMap.containsKey(activityId)) {
                removeChefDuel2Data(activityId);
            }
        }
        for (ChefDuel3 chefDuel3:chefDuel3Map.values()){
            int activityId = chefDuel3.getActivityInfo().getActivityId();
            if (!activityInfoMap.containsKey(activityId)) {
                removeChefDuel3Data(activityId);
            }
        }
    }


    /**
     * 移除第一阶段数据
     */
    private static void removeChefDuelData(int activityId){
        chefDuel1Map.remove(activityId);
        activityLock.remove(activityId);
        log.error("移除厨神对决一阶段活动 id:{}",activityId);
    }

    /**
     * 移除第二阶段数据
     */
    private static void removeChefDuel2Data(int activityId){
        chefDuel2Map.remove(activityId);
        activityLock.remove(activityId);
        log.error("移除厨神对决二阶段活动 id:{}",activityId);
    }

    /**
     * 移除第三阶段数据
     */
    private static void removeChefDuel3Data(int activityId){
        chefDuel3Map.remove(activityId);
        activityLock.remove(activityId);
        log.error("移除厨神对决第三阶段活动 id:{}",activityId);
    }



    /**
     * 刷新活动
     */
    public static void reloadActivity() {
        Map<Integer, ActivityInfo> activityInfoMap = RelationActivityMgr.getOpenActivityInfoMap(eActivityType.ChefDuel.getValue());
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()){
//                reloadChefDuel1(activityInfo.getActivityId());
            }else if (activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()){
                reloadChefDuel2(activityInfo.getActivityId());
            }else if (activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()){
                reloadChefDuel3(activityInfo.getActivityId());
            }
        }
    }


    /**
     * 加载二阶段
     */
    public static void reloadChefDuel2(int activityId) {
        ActivityInfo activityInfo = ChefDuelActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            if (chefDuel2Map.containsKey(activityId)) {
                chefDuel2Map.get(activityId).setActivityInfo(activityInfo);
            }else {
                ChefDuel2 c2 = new ChefDuel2(activityInfo);
                if (c2.reload()){
                    chefDuel2Map.put(activityId, c2);
                }else {
                    log.error("厨神对决二阶段"+activityId+"加载失败");
                }
            }
        }else {
            log.error("厨神对决二阶段活动数据info未找到"+activityId+"加载失败");
        }
    }

    /**
     * 加载三阶段
     */
    public static void reloadChefDuel3(int activityId) {
        ActivityInfo activityInfo = ChefDuelActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            if (chefDuel3Map.containsKey(activityId)) {
                chefDuel3Map.get(activityId).setActivityInfo(activityInfo);
            }else {
                ChefDuel3 c3 = new ChefDuel3(activityInfo);
                if (c3.reload()){
                    chefDuel3Map.put(activityId, c3);
                }else {
                    log.error("厨神对决三阶段"+activityId+"加载失败");
                }
            }
        }else {
            log.error("厨神对决三阶段活动数据info未找到"+activityId+"加载失败");
        }
    }

    /**
     * 获取活动锁
     *
     * @param activityId 活动ID
     * @return
     */
    private static synchronized Object getActivityLock(int activityId) {
        if (activityLock == null) {
            activityLock = new ConcurrentHashMap<>();
        }
        if (!activityLock.containsKey(activityId)) {
            activityLock.put(activityId, new Object());
        }
        return activityLock.get(activityId);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }


    @Override
    public boolean save() {
        for (ChefDuel2 chefDuel2 : chefDuel2Map.values()) {
            chefDuel2.save();
        }
        for (ChefDuel3 chefDuel3 : chefDuel3Map.values()) {
            chefDuel3.save();
        }
        return true;
    }

    /**
     * 二、三阶段
     * ===================================================
     */
    //定时处理，活动到期菜品转换成兑换积分
    public static void timerTask() {
        for (ChefDuel2 chefDuel2 : chefDuel2Map.values()) {
            synchronized (getActivityLock(chefDuel2.getActivityInfo().getActivityId())) {
                chefDuel2.timerTask();
            }
        }

    }

    public static void timerTask2() {
        int currentSecond = DateHelper.getCurrentSecond();
        for (ChefDuel2 chefDuel2 : chefDuel2Map.values()) {
            if(currentSecond > chefDuel2.getActivityInfo().getBeginTime() && currentSecond < chefDuel2.getActivityInfo().getEndTime() + 4 * DateHelper.MINUTE_SECONDS){
                chefDuel2.checkUpUserScoreUpload();
            }
        }

    }

    public static void timerTask3() {
        int currentSecond = DateHelper.getCurrentSecond();
        for (ChefDuel3 chefDuel3 : chefDuel3Map.values()) {
            if(currentSecond > chefDuel3.getActivityInfo().getBeginTime() && currentSecond < chefDuel3.getActivityInfo().getEndTime() + 4 * DateHelper.MINUTE_SECONDS){
                chefDuel3.checkUpUserScoreUpload();
            }
        }

    }


    public static void syncChefDuelWindUp(int activityId, List<ChefDuelProto.CrossChefDuelWindUpCookerTempMsg> userIdList) {
        Map<Long, ChefDuelData> windUpUserChefDuel1Map = new ConcurrentHashMap<>();
        synchronized (getActivityLock(activityId)) {
//            ChefDuel1 chefDuel = getChefDuel(activityId);
//            if (chefDuel != null) {
//                windUpUserChefDuel1Map = chefDuel.getWindUpUserChefDuelMap(userIdList);
//            }
        }
        ActivityInfo ck1Ac = ChefDuelActivityMgr.getOpenActivityInfo(activityId);
        if(ck1Ac == null){
            ck1Ac = ActivityMgr.getOpenActivityInfo(activityId);
        }
        List<ActivityInfo> activityInfoList = ChefDuelActivityMgr.getOpenActivityInfoList(eActivityType.ChefDuel.getValue(), eChefDuelActivityType.Phase2.getChildType());
        for (ActivityInfo activityInfo : activityInfoList) {
            //阶段2活动和阶段1活动有重叠或者只有一个活动
            if (ChefDuelActivityMgr.isActivityContain(ck1Ac, activityInfo)) {
                if (activityInfo.getServerIdList().contains(GameServer.getInstance().getServerId())) {
                    synchronized (getActivityLock(activityInfo.getActivityId())) {
                        ChefDuel2 chefDuel2 = getChefDuel2(activityInfo.getActivityId());
                        if (chefDuel2 != null) {
                            chefDuel2.addPromotionUserList(activityId, userIdList, windUpUserChefDuel1Map);
                        }
                    }
                }
            }
        }
    }

    public static void syncChefDuel2WindUp(int activityId, List<ChefDuelProto.CrossChefDuelWindUpCookerTempMsg> userIdList) {
        Map<Long, ChefDuelData> windUpUserChefDuel2Map = new ConcurrentHashMap<>();
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 chefDuel2 = getChefDuel2(activityId);
            if (chefDuel2 != null) {
                windUpUserChefDuel2Map = chefDuel2.getWindUpUserChefDuelMap(userIdList);
            }
        }
        ActivityInfo ck1Ac = ChefDuelActivityMgr.getOpenActivityInfo(activityId);
        List<ActivityInfo> activityInfoList = ChefDuelActivityMgr.getOpenActivityInfoList(eActivityType.ChefDuel.getValue(), eChefDuelActivityType.Phase3.getChildType());
        for (ActivityInfo activityInfo : activityInfoList) {
            //阶段2活动和阶段1活动有重叠或者只有一个活动
            if (ChefDuelActivityMgr.isActivityContain(ck1Ac, activityInfo)) {
                if (activityInfo.getServerIdList().contains(GameServer.getInstance().getServerId())) {
                    synchronized (getActivityLock(activityInfo.getActivityId())) {
                        ChefDuel3 chefDuel3 = getChefDuel3(activityInfo.getActivityId());
                        if (chefDuel3 != null) {
                            chefDuel3.addPromotionUserList(userIdList, windUpUserChefDuel2Map);
                        }
                    }
                }
            }
        }
    }

    public static void selectCampId(int activityId, int campId, long playerId) {
        ChefDuel2 chefDuel2 = getChefDuel2(activityId);
        if(chefDuel2 != null){
            chefDuel2.selectCamp(playerId, campId);
        }
    }

    public static void supportCampId3(int activityId, int campId, long playerId) {
        ChefDuel3 chefDuel3 = getChefDuel3(activityId);
        if(chefDuel3 != null){
            chefDuel3.supportCamp(playerId, campId);
        }
    }

    /*晋级↑↑↑↑↑↑*/
    public static void openBanquetAddCondition(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                chefDuel3.openBanquetAddCondition(userId);
            }
        }
    }

    public static int sendOpenTable(int activityId, long userId, int itemId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                return chefDuel3.sendOpenTable(userId, itemId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendCooker3Msg(int activityId, long userId, long homeUserId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                return chefDuel3.sendCookerMsg(userId, homeUserId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int worship3(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                return chefDuel3.worship(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static void syncChefDuel3Table(int activityId, ChefDuelProto.CrossChefDuel3TableSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                chefDuel3.syncChefDuel3Table(msg);
            }
        }
    }

    public static void syncChefDuel3BanquetUserData(int activityId, ChefDuelProto.ChefDuel3UserRewardSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                chefDuel3.syncChefDuel3BanquetUserData(msg);
            }
        }
    }

    public static void syncChefDuel3BanquetOpenNotice(int activityId, ChefDuelProto.ChefDuel3BanquetOpenSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                chefDuel3.syncChefDuel3BanquetOpenNotice(msg);
            }
        }
    }

    //参加宴会
    public static int sendJoinBanquetMsg(int activityId, long userId, long homeUserId, int banquetId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                return chefDuel3.sendJoinBanquetMsg(userId, homeUserId, banquetId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static void chefDuel3openerBackMsg(int activityId, long userId, int banquetItemId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                chefDuel3.chefDuel3OpenerBackMsg(userId, banquetItemId);
            }
        }
    }

    public static String chefDuel3joinerBackMsg(int activityId, long userId, int banquetItemId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                return chefDuel3.ChefDuel3joinerBackMsg(userId, banquetItemId);
            }
        }
        return "";
    }

    public static void chefDuel3RobotBackMsg(int activityId, long userId, long integral) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 chefDuel3 = getChefDuel3(activityId);
            if (chefDuel3 != null) {
                chefDuel3.chefDuel3RobotBackMsg(userId, integral);
            }
        }
    }

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓厨神对决2↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
    public static void chefDuel2RobotBackMsg(int activityId, ChefDuelProto.CrossChefDuel2RobotJoinBanquetMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 chefDuel2 = getChefDuel2(activityId);
            if (chefDuel2 != null) {
                chefDuel2.chefDuel2RobotBackMsg(msg);
            }
        }
    }

    public static void syncHorseNotice(int activityId, ChefDuelProto.CrossChefDuel2HorseSyncServerMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncHorseNotice(msg);
            }
        }
    }

    public static void syncTable(int activityId, ChefDuelProto.CrossChefDuel2TableSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncTable(msg);
                return;
            }
        }
        //同步过来的是赴宴范围的桌子
        OtherActivityChefDuel2 cookBoy2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(activityId);
        if(cookBoy2 != null){
            cookBoy2.syncTable(msg);
        }else {
            getLogger().info("厨神对决2阶段，赴宴范围活动{}，缺失活动基础配置", activityId);
        }
    }

    public static void syncFoodServe(int activityId, long userId, ChefDuelProto.ChefDuel2OpenTableRespMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncFoodServe(userId, msg);
            }
        }
    }

    public static void backFoodServe(int activityId, long userId, ChefDuelProto.ChefDuel2OpenTableRespMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.backFoodServe(userId, msg);
            }
        }
    }

    public static void syncCooker(int activityId, ChefDuelProto.CrossChefDuel2CookerSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncCooker(msg);
                return;
            }
        }
        //同步过来的是赴宴范围的桌子
        OtherActivityChefDuel2 cookBoy2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(activityId);
        if(cookBoy2 != null){
            cookBoy2.syncCooker(msg);
        }else {
            getLogger().info("厨神对决2阶段，赴宴范围活动{}，缺失活动基础配置", activityId);
        }
    }

    public static void openerBackMsg(int activityId, long userId, int spoonId, int tableId, int autoJoin) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.openerBackMsg(userId, spoonId, tableId, autoJoin);
            }
        }
    }

    public static String joinerBackMsg(int activityId, long userId, int spoonId, int autoJoin) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.joinerBackMsg(userId, spoonId, autoJoin);
            }
        }
        return "";
    }

    public static void sendCookerHomeMsg(int activityId, long userId, long homeUser, int page, int num) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.sendCookerHomeMsg(userId, homeUser, page, num);
                return;
            }
        }
        //操作赴宴范围的桌子
        OtherActivityChefDuel2 cookBoy2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(activityId);
        if(cookBoy2 != null){
            cookBoy2.sendCookerHomeMsg(userId, homeUser, page, num);
        }
    }

    public static void sendCookerTableMsg(int activityId, long userId, long homeUser, int tableId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.sendCookerTableMsg(userId, homeUser, tableId);
            }
        }
    }

    //参加宴会
    public static int sendJoinMsg(int activityId, long userId, long honeUserId, int tableId, int spoonId, int trueActivityId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                ChefDuelData duelData = cookBoy2.getChefDuelDataMap().get(userId);
                return cookBoy2.sendJoinMsg(userId, honeUserId, tableId, spoonId, trueActivityId, duelData.getCampId());
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }
    //预参加宴会
    public static int sendPreJoinMsg(int activityId, long userId, long honeUserId, int tableId, int spoonId, int trueActivityId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                ChefDuelData duelData = cookBoy2.getChefDuelDataMap().get(userId);
                return cookBoy2.sendPreJoinMsg(userId, honeUserId, tableId, spoonId, trueActivityId, duelData.getCampId());
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    //上菜开桌子
    public static int sendServeDish(int activityId, long userId, List<Integer> dishIdList) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendServeDish(userId, dishIdList);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendForbidSpoon(int activityId, long userId, List<Integer> spoonIdList) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendForbidSpoon(userId, spoonIdList);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendCookerMsg(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendCookerMsg(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendRewardMsg(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendRewardMsg(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendRelationMsg(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendRelationMsg(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendCanEastSearchMsg(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendCanEastSearchMsg(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendSearchUserMsg(int activityId, long userId, String searchId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendSearchUserMsg(userId, searchId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static PickDishRes pickDish(int activityId, long userId, int times) {
        PickDishRes pickDishRes = new PickDishRes();
        pickDishRes.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.newPickDish(userId, times);
            }
        }
        return pickDishRes;
    }

    public static List<ChefDuel2Dish> dishMerge(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.dishMerge(userId);
            }
        }
        return null;
    }

    public static void addSceneUser(int activityId, long userId, long openBanquetUserId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 cookBoy = getChefDuel3(activityId);
            if (cookBoy != null) {
                cookBoy.addSceneUser(userId, openBanquetUserId);
            }
        }
    }

    public static void addChefDuel2SceneUser(int activityId, long userId, long openBanquetUserId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.addSceneUser(userId, openBanquetUserId);
                return;
            }
        }
        //操作赴宴范围的桌子
        OtherActivityChefDuel2 cookBoy2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(activityId);
        if(cookBoy2 != null){
            cookBoy2.addSceneUser(userId, openBanquetUserId);
        }
    }

    public static void removeChefDuel2SceneUser(int activityId, long userId, long openBanquetUserId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.removeSceneUser(userId, openBanquetUserId);
            }
        }
    }

    public static int worship2(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.worship(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }
    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓厨神对决1↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    public static int worship(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
//            ChefDuel1 cookBoy = getChefDuel(activityId);
//            if (cookBoy != null) {
//                return cookBoy.worship(userId);
//            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static void removeSceneUser(int activityId, long userId, long openBanquetUserId) {
        ActivityInfo activityInfo = RelationActivityMgr.getActivityInfo(activityId);
        if (activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()) {
            synchronized (getActivityLock(activityId)) {
//                ChefDuel1 cookBoy = getChefDuel(activityId);
//                if (cookBoy != null) {
//                    cookBoy.removeSceneUser(userId, openBanquetUserId);
//                }
            }
        } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()) {
            synchronized (getActivityLock(activityId)) {
                ChefDuel2 cookBoy2 = getChefDuel2(activityId);
                if (cookBoy2 != null) {
                    cookBoy2.removeSceneUser(userId, openBanquetUserId);
                }
            }
        } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()) {
            synchronized (getActivityLock(activityId)) {
                ChefDuel3 cookBoy3 = getChefDuel3(activityId);
                if (cookBoy3 != null) {
                    cookBoy3.removeSceneUser(userId, openBanquetUserId);
                }
            }
        }

    }

//    public static void updateMatchOverFood(int activityId, ChefDuelProto.CrossChefDuelMatchOverRewardSyncMsg msg) {
//        synchronized (getActivityLock(activityId)) {
//            ChefDuel1 cookBoy = getChefDuel(activityId);
//            if (cookBoy != null) {
//                cookBoy.updateMatchOverFood(msg);
//            }
//        }
//    }

//    public static void syncMatch(int activityId, ChefDuelProto.CrossChefDuelMatchSyncMsg msg) {
//        synchronized (getActivityLock(activityId)) {
//            ChefDuel1 cookBoy = getChefDuel(activityId);
//            if (cookBoy != null) {
//                cookBoy.syncMatch(msg);
//            }
//        }
//    }

    public static ChefDuelData getUserChefDuel2DataNoLock(int activityId, GamePlayer player) {
        ChefDuel2 chefDuel2 = getChefDuel2NoInit(activityId);
        if (chefDuel2 != null) {
            return chefDuel2.getUserChefDuelData(player);
        }
        return null;
    }

    public static ChefDuelData getUserChefDuel3DataNoLock(int activityId, GamePlayer player) {
        ChefDuel3 chefDuel3 = getChefDuel3NoInit(activityId);
        if (chefDuel3 != null) {
            return chefDuel3.getUserChefDuelData(player);
        }
        return null;
    }

    //加锁用户登陆会有GamePlayerMgr.getPlayerLock锁和getActivityLock(activityId)锁，timerTask会有getActivityLock(activityId)锁和发邮件的GamePlayerMgr.getPlayerLock锁
    public static ChefDuelData getUserChefDuel2Data(int activityId, GamePlayer player) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.getUserChefDuelData(player);
            }
        }
        return null;
    }

    public static ChefDuelData getUserChefDuel3Data(int activityId, GamePlayer player) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 cookBoy3 = getChefDuel3(activityId);
            if (cookBoy3 != null) {
                return cookBoy3.getUserChefDuelData(player);
            }
        }
        return null;
    }

    public static void setUserChefDuel3Able(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel3 cookBoy3 = getChefDuel3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.setUserChefDuel3Able(userId);
            }
        }
    }

    public static boolean isUserChefDuelAble(int activityId, long userId) {
        ActivityInfo activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if (activityInfo != null) {
            synchronized (getActivityLock(activityId)) {
                if (activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()) {
//                    ChefDuel1 cookBoy = getChefDuel(activityId);
//                    if (cookBoy != null) {
//                        return cookBoy.isUserChefDuelAble(userId);
//                    }
                } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()) {
                    ChefDuel2 cookBoy2 = getChefDuel2(activityId);
                    if (cookBoy2 != null) {
                        return cookBoy2.isUserChefDuel2Able(userId);
                    }
                } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()) {
                    ChefDuel3 cookBoy3 = getChefDuel3(activityId);
                    if (cookBoy3 != null) {
                        return cookBoy3.isUserChefDuel3Able(userId);
                    }
                }
            }
        }
        return false;
    }

    private static ChefDuel3 getChefDuel3NoInit(int activityId) {
        if (chefDuel3Map.containsKey(activityId)) {
            return chefDuel3Map.get(activityId);
        }
        return null;
    }
    private static ChefDuel2 getChefDuel2NoInit(int activityId) {
        if (chefDuel2Map.containsKey(activityId)) {
            return chefDuel2Map.get(activityId);
        }
        return null;
    }

    private static ChefDuel2 getChefDuel2(int activityId) {
        if (chefDuel2Map.containsKey(activityId)) {
            return chefDuel2Map.get(activityId);
        } else {
            ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                ChefDuel2 cookBoy2 = new ChefDuel2(acInfo);
                if (cookBoy2.init()) {
                    chefDuel2Map.put(activityId, cookBoy2);
                } else {
                    log.error("厨神对决2活动" + activityId + "加载失败");
                }
                return chefDuel2Map.get(activityId);
            }
        }
        return null;
    }

    private static ChefDuel3 getChefDuel3(int activityId) {
        if (chefDuel3Map.containsKey(activityId)) {
            return chefDuel3Map.get(activityId);
        } else {
            ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                ChefDuel3 cookBoy3 = new ChefDuel3(acInfo);
                if (cookBoy3.init()) {
                    chefDuel3Map.put(activityId, cookBoy3);
                } else {
                    log.error("厨神对决3活动" + activityId + "加载失败");
                }
                return chefDuel3Map.get(activityId);
            }
        }
        return null;
    }

    public static Map<Integer, ChefDuel2> getChefDuel2Map() {
        return chefDuel2Map;
    }

    public static Map<Integer, ChefDuel3> getChefDuel3Map() {
        return chefDuel3Map;
    }


    /**
     * 获取自动回访记录
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @return ret
     */
    public static int getAutoJoinRecord(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.getAutoJoinRecord(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    /**
     * 设置自动回访汤勺
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @param spoons     汤勺
     * @return ret
     */
    public static int setAutoJoinSpoon(int activityId, long userId, List<ChefDuelProto.ChefDuel2SpoonTempMsg> spoons) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.setAutoJoinSpoon(activityId, userId, spoons);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    /**
     * 获取自动回访汤勺
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @return ret
     */
    public static int getAutoJoinSpoon(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.getAutoJoinSpoon(activityId, userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    /**
     * 处理自动回访剩余的汤勺
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @param records    回访记录
     */
    public static void dealAutoJoinSpoonRest(int activityId, long userId, List<ChefDuelProto.ChefDuel2SpoonTempMsg> records) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.dealAutoJoinSpoonRest(userId, records);
            }
        }
    }

    /**
     * 开始自动回访流程
     *
     * @param activityId 活动ID
     * @param reqMsg     reqMsg
     */
    public static void startAutoJoin(int activityId, ChefDuelProto.CrossChefDuel2AutoJoinInfoMsg reqMsg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.startAutoJoin(reqMsg);
            }
        }
    }

    /**
     * 正式进行试吃
     *
     * @param activityId 活动ID
     */
    public static void autoJoinTaste(int activityId, ChefDuelProto.CrossChefDuel2SyncTasteTableCntMsg reqMsg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.autoJoinTaste(reqMsg);
            }
        }
    }

    /**
     * 同步自动回访结果
     *
     * @param activityId 活动ID
     * @param reqMsg     reqMsg
     */
    public static void dealAutoJoinResult(int activityId, ChefDuelProto.CrossChefDuel2SyncAutoJoinResultMsg reqMsg) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.dealAutoJoinResult(reqMsg);
            }
        }
    }


    /**
     * 处理自动回访收益
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @param returnStr  回访收益
     * @param spoonId    汤勺ID
     * @param count      回访次数
     */
    public static void dealAutoJoinReward(int activityId, long userId, String returnStr, int spoonId, int count) {
        synchronized (getActivityLock(activityId)) {
            ChefDuel2 cookBoy2 = getChefDuel2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.dealAutoJoinReward(userId, returnStr, spoonId, count);
            }
        }
    }
}
